# 导入三个包time，datetime，timedelta
import time
from datetime import datetime
from datetime import timedelta

# time
# 一、查看时间

# 1.获取当前本地时间,精度到秒
my_localtime = time.localtime()
print(f"1.当前本地时间\n{my_localtime}")

# 2.改变时区,通常使用datetime,了解即可
# import os
# os.environ["TZ"] = "US"
# print(time.tzset())

# 3.时间戳timestamp
# 单位是秒:从1970/1/1开始经过的秒数,精度是微妙
# UTC指通用时间,没有时区
my_time = time.time()
print(f"2.时间戳\n{my_time}\n")

# 二、时间格式转换
# 1.struct_time与str转换
# time.struct_time->str
my_strtime = time.strftime("%Y-%m-%d %H:%M:%S", my_localtime)
print(f"3.struct_time转为str\n{my_strtime}")
# str->time.struct_time
my_struct_time_1 = time.strptime("2020-11-11 15:43:31", "%Y-%m-%d %H:%M:%S")
print(f"4.str转为struct_time\n{my_struct_time_1}\n")

# 2.struct_time与tuple转换
# time.struct_time->tuple
my_tupletime = tuple(my_localtime)
print(f"5.struct_time转为tuple\n{my_tupletime}")
# tuple->time.struct_time
my_struct_time_2 = time.struct_time((2020, 11, 11, 15, 43, 4, 2, 316, 0))
print(f"6.tuple转为struct_time\n{my_struct_time_2}\n")

# 3.struct_time与timestamp转换
# timestamp->time.struct_time
# 有时区
my_struct_time_3 = time.localtime(1605151818.6700244)
print(f"7.timestamp转为struct_time有时区\n{my_struct_time_3}")
# 没有时区
my_struct_time_4 = time.gmtime(1605151818.6700244)
print(f"8.timestamp转为struct_time无时区\n{my_struct_time_4}")
# time.struct_time->timestamp会丢失时间精度
my_timestamp = time.mktime(my_localtime)
print(f"9.struct_time转为timestamp\n{my_timestamp}\n")

# 三、时间运算
# 只能通过时间格式到元组,然后创建新元组的方式新建新的时间

# 四、时间休眠，设置停顿1s
time.sleep(1)

# datetime
# 一、查看时间
# 1.查看时间
my_today_1 = datetime.today()
print(f"1.查看时间\n{my_today_1}")
# 2.可以指定时区查看时间
my_now = datetime.now(tz=None)
print(f"2.查看指定时区时间\n{my_now}")
# 3.查看UTC时间
my_utcnow = datetime.utcnow()
print(f"3.查看UTC时间\n{my_utcnow}\n")

# 二、时间格式的转换
# 1.datetime与str转换
# datetime.datetime->str
now_str = my_now.strftime("%Y-%m-%d %H:%M:%S")
print(f"4.datetime转为str\n{now_str}, {type(now_str)}")
# str->datetime.datetime
now_1 = datetime.strptime(now_str, "%Y-%m-%d %H:%M:%S")
print(f"5.str转为datetime\n{now_1}, {type(now_1)}\n")

# 2.datetime与tuple转换
# datetime.datetime->timetuple->tuple
my_tupletime_1 = tuple(now_1.timetuple())
print(f"6.datetime转为tuple\n{my_tupletime_1}")
# tuple->datetime.datetime
new_t = my_tupletime_1[:-3]
datetime_2 = datetime(*new_t)
print(f"7.tuple转为datetime\n{datetime_2}\n")

# 3.datetime与timestamp转换
# datetime.datetime->timestamp
my_timestamp_1 = now_1.timestamp()
print(f"8.datetime转为timestamp\n{my_timestamp_1}")
# timestamp->datetime.datetime
datetime_3 = datetime.fromtimestamp(my_timestamp_1)
print(f"9.timestamp转为datetime\n{datetime_3}\n")

# 三、时间运算
now_1 + timedelta(hours=-1)
print(f"10.往前推迟一小时\n{now_1}\n")


# 作业
# 封装一个函数get_date(days) , 通过传入的数值days(days=1[-2,3,4,5]), 返回格式化后的时间2020-11-05
def get_date(days):
    my_today = datetime.now(tz=None)
    my_today += timedelta(hours=int(days) * 24)
    my_today = my_today.strftime("%Y-%m-%d")
    if int(days) > 0:
        print(f"{int(days)}天后是\n{my_today}")
    elif int(days) < 0:
        print(f"{abs(int(days))}天前是\n{my_today}")
    else:
        print(f"今天是\n{my_today}")
    return None


if __name__ == '__main__':
    get_date(-1)
